package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.sdk.app.PayTask;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.demo.PayResult;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

public class MineAccount extends BaseActivity implements View.OnClickListener{
    private ImageView accountback;
    private TextView outmoney;
    private TextView joinmoney;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private String data;
    private TextView baozhengno;
    private TextView yajin;
    public TextView notext;
    private String amount;
    private String caution;
    private String service;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    // 更新ui
                    notext.setText(amount);
                    break;
            }
        }
    };
    private TextView lastmoney;
    private int i;
    private String identityStatus;
    private RelativeLayout accountpsd;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_account);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "0");
        userId = sharedPreferences.getString("userId", "1");
        token = sharedPreferences.getString("token", "2");
        identityStatus = getIntent().getStringExtra("identityStatus");
        initMineAccountView();

        //获取账户信息
        new Thread(new Runnable() {
            @Override
            public void run() {

                getAcountData();
            }
        }).start();
        long l = System.currentTimeMillis();
        Date date = new Date(l);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        i = calendar.get(calendar.DAY_OF_WEEK);
        
    }

    private void getAcountData() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+"/account/getAccountInfo");
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
                String data = obj.getString("data");
                com.alibaba.fastjson.JSONObject obj1 = JSON.parseObject(data);
                amount = obj1.getString("amount");
                caution = obj1.getString("caution");
                service = obj1.getString("service");
                handler.sendEmptyMessage(1);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(MineAccount.this,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initMineAccountView() {
        accountpsd = (RelativeLayout) findViewById(R.id.account_psd);
        accountback = (ImageView) findViewById(R.id.mine_account_back);
        outmoney = (TextView) findViewById(R.id.mine_account_outmoney);
        joinmoney = (TextView) findViewById(R.id.mine_account_joinmoney);
        lastmoney = (TextView) findViewById(R.id.mine_acount_lastmoney);

        notext = (TextView) findViewById(R.id.account_notext);
        backmain = (ImageView) findViewById(R.id.mineaccount_backmain);

        // 设置点击事件监听
        backmain.setOnClickListener(this);
         accountback.setOnClickListener(this);
        outmoney.setOnClickListener(this);
        joinmoney.setOnClickListener(this);
        lastmoney.setOnClickListener(this);
        accountpsd.setOnClickListener(this);
    }
        // 设置点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.mineaccount_backmain:
                startActivity(new Intent(MineAccount.this,MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.account_psd:
                startActivity(new Intent(MineAccount.this,MinePayPasswordActivity.class));
                break;
            case R.id.mine_account_back:// 点击返回
                finish();
                break;
            case R.id.mine_account_joinmoney:// 点击充值
                startActivity(new Intent(this,MineAccountJoinMoney.class));
                break;
            case R.id.mine_account_outmoney:// 点击提现
                if (i == 2){
                    if (identityStatus==null){
                        Toast.makeText(this, "为了您的财产安全，请进入个人中心进行实名认证", Toast.LENGTH_SHORT).show();
                    }else {
                        if (identityStatus.equals("1")){
                            startActivity(new Intent(this,MineAccountOutMoney.class));
                        }else {
                            Toast.makeText(this, "为了您的财产安全，请进入个人中心进行实名认证", Toast.LENGTH_SHORT).show();
                        }
                    }
                }else {
                    Toast.makeText(this, "亲，提现申请时间为每周一哦，周二对账，周三打款", Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.mine_acount_lastmoney:
                startActivity(new Intent(MineAccount.this,MyAccountStream.class));
                break;
        }
    }

}
